package net.melon.cata.manager.id;

import net.esj.basic.utils.Assert;
import net.melon.cata.manager.exception.OverMaxCountException;
import net.melon.cata.pojo.Catalog;

/**
 * 十六进制ID生成规则：
 * child = parent*256+1
 * 每层255个
 * @author Administrator
 *
 */
public class HexaIDGenerate implements ScaleIDGenerate {

	@Override
	public synchronized Long generateBrother(Long brotherId, Catalog catalog) throws OverMaxCountException {
		Assert.notNull(brotherId);
		Long id = brotherId%256L;
		if(id==255){
			throw new OverMaxCountException("over max count 255");
		}else{
			id = brotherId+1;
		}
		if(catalog==null){
			return id;
		}else{
			return catalog.getId()*255+ id;
		}
	}

	@Override
	public synchronized Long generateFirstChild(Long parentId) {
		Assert.notNull(parentId);
		Long id = Long.valueOf(parentId<<8);
		id ++;
		return id;
	}

	@Override
	public Long getParent(Long childId) {
		Assert.notNull(childId);
		Long id = childId/256L;
		return id;
	}

	public static void main(String[] args){
		Long id = (long) (3<<8);
		System.out.println(id);
	}
}
